var fs = require('fs');

exports.checkUser = function(user){
    if(user == undefined){
        return false;
    }

    return true;
};

exports.getErrMsgByRecord = function(entityName, record, msg){
    var newMsg = "";    //"表名：" + entityName + "\n";
    if(record.Name != undefined){
        newMsg += "名称：" + record.Name + "\n";
    }
    if(record.Code != undefined){
        newMsg += "编码：" + record.Code + "\n";
    }
    if(newMsg === ""){
        newMsg += "ID：" + record._id + "\n";
    }

    newMsg += "信息：\n  " + msg;
    return newMsg;
};

exports.authorizedCheck = function(user, res){
    if(!this.checkUser(user)){
        var newErr = new Error();
        newErr.leval = 0;
        newErr.title = '用户验证失败';
        newErr.message = '登录超时，请重新登录';
        res.status(400).send(newErr);
        res.ends();
    }
};

exports.responseHandler = function(res, leval, title) {
    return function (err, result) {
        if (err) {
            if(err.leval == undefined || err.leval == null) {
                if (leval != undefined) {
                    err.leval = leval;
                }
                else {
                    err.leval = 9;
                }
            }

            if(err.title == undefined || err.title == null) {
                if (title != undefined) {
                    err.title = title;
                }
                else {
                    err.title = '系统错误';
                }
            }

            exports.errorLog(err.title, err.message, res.req.user.Code);
            res.status(400).send(err);
        } else {
            res.json({Data: result});
        }
    };
};

//将条件进行格式化，将传过来的正则表达式字符串转换为正则表达式
exports.formatCondition = function(condition){
    for(var p in condition){
        if(condition[p] != null) {
            if (condition[p].constructor == String) {
                if (condition[p].search(/^\/.*\/$/) > -1) {
                    condition[p] = new RegExp(condition[p].substring(1, condition[p].length - 1));
                }
            }
            else if (condition[p].constructor == Object) {
                condition[p] = this.formatCondition(condition[p]);
            }
        }
    }

    return condition;
};

//记录错误日志
exports.errorLog = function(title, msg, strUser){
    var strMsg;
    var nowDate = new Date();
    strMsg = nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1) + '-' + nowDate.getDate() + ' ' + nowDate.getHours() + ":" + nowDate.getMinutes() + ":" + nowDate.getSeconds();
    if(strUser != undefined && strUser.trim() != ''){
        strMsg += ' 【' + strUser + '】';
    }
    strMsg += '\r\n标题：' + title + '\r\n' + '错误信息：' + msg + '\r\n\r\n';

    errorLogfile = fs.createWriteStream('./logs/error.log', { flags: 'a' });    //打开错误日志文件
    errorLogfile.write(strMsg);
};

//记录访问日志
exports.accessLog = function(title, msg, strUser){
    var strMsg;
    var nowDate = new Date();
    strMsg = nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1) + '-' + nowDate.getDate() + ' ' + nowDate.getHours() + ":" + nowDate.getMinutes() + ":" + nowDate.getSeconds();
    if(strUser != undefined && strUser.trim() != ''){
        strMsg += ' 【' + strUser + '】';
    }
    strMsg += '标题：' + title + '\r\n' + '错误信息：' + msg + '\r\n\r\n';

    accessLogfile = fs.createWriteStream('./logs/access.log', {flags: 'a'});    //打开访问日志文件
    accessLogfile.write(strMsg);
};
